/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Blank_Grade_Concession_warn",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.DomainModels;
using ATkg.BizAppProject.IServices;
using Microsoft.AspNetCore.Authorization;

namespace ATkg.BizAppProject.Controllers
{
    public partial class Blank_Grade_Concession_warnController
    {
        private readonly IBlank_Grade_Concession_warnService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public Blank_Grade_Concession_warnController(
            IBlank_Grade_Concession_warnService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        // 将网页中填写的审核信息传入数据库
        [HttpPost("GetInfoFromMesAndDeal")]
        [AllowAnonymous]
        public async Task<JsonResult> GetInfoFromMesAndDeal()
        {
            try
            {

                var r1 = await Service.GetDataFromMes();  //从MES获取最新数据并写入数据库中
                var r = await Service.GetDataFromDbAndDeal();  //从数据库中获取数据并判断是否发送预警邮件
                // var r = new { Success = true, Message = "处理成功" };
                return new JsonResult(new
                {
                    Success = true,
                    Message = "处理成功！",
                    Data = r  // 新增：返回string格式的时间
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }

        [HttpGet("GetDateFromDb")]
        public async Task<JsonResult> GetDateFromDb()
        {
            try
            {
                var r = await Service.GetDateFromDb();
                return new JsonResult(new
                {
                    Success = true,
                    Message = "数据处理成功",
                    Data = r  // 新增：返回string格式的时间
                });

            }
            catch (Exception ex)
            {
                // 返回错误的 JsonResult
                return new JsonResult(new { Success = false, Message = $"处理失败: {ex.Message}" })
                {
                    StatusCode = 500 // 设置 HTTP 状态码
                };
            }


        }
    }
}
